package utils;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.util.Log;

import java.io.IOException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketAddress;
import java.net.UnknownHostException;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Looper;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.content.BroadcastReceiver;



public class Utils {

	private Context context;
	private static final String NombrePref = "DondeEstoyPref";
	public static int MOVIL_LOCATION_TYPE_ID = 5;
	public static int PERSONAL_LOCATION_CATEGORY_ID = 5;
	public static Boolean serviceStart=true;
	String strLevelbat;
	
	public Utils(Context context) {
		this.context = context;
	}
	
	public void saveIMEI(String imei) {
		SharedPreferences settings = context.getSharedPreferences(NombrePref, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = settings.edit();
        editor.putString("IMEI", imei);
        editor.commit();
	}
	
	public String getIMEI() {
		SharedPreferences settings = context.getSharedPreferences(NombrePref, Context.MODE_PRIVATE);
		return settings.getString("IMEI", "");
	}
	
	public void saveServerAddress(String serverAddres) {
		SharedPreferences settings = context.getSharedPreferences(NombrePref, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = settings.edit();
        editor.putString("ServerAddres", serverAddres);
        editor.commit();
	}
	
	public String getServerAddress() {
		SharedPreferences settings = context.getSharedPreferences(NombrePref, Context.MODE_PRIVATE);
		return settings.getString("ServerAddres", "sharedpc.dyndns-home.com:5123");
	}
	
////////////////////////////////////////////////BATTERY &////////////////////////////////////////
public String getBatteryLevel() {
	
	BroadcastReceiver batteryLevelReceiver = new BroadcastReceiver() {
	
		public void onReceive(Context context, Intent intent) {	
			context.unregisterReceiver(this);
			int rawlevel = intent.getIntExtra("level", -1);		
			int scale = intent.getIntExtra("scale", -1);		
			int levelbat = -1;		
			if (rawlevel >= 0 && scale > 0) {		
				levelbat = (rawlevel * 100) / scale;	
			}
			//Log.i(TAG, "[strbatteryLevel] BATTERY int: " + levelbat);
			strLevelbat=String.valueOf(levelbat);
			Log.i("strBatteryLevel", "[strbatteryLevel] BATTERY str: " + strLevelbat);	
		}
	};
	
	IntentFilter batteryLevelFilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);    	
	this.context.getApplicationContext().registerReceiver(batteryLevelReceiver, batteryLevelFilter);
	Log.i("strBatteryLevel", "[strbatteryLevel] BATTERY str final: " + strLevelbat);	
	
	return strLevelbat;
	}

	public String getIdEquipo() {
		SharedPreferences settings = context.getSharedPreferences(NombrePref, Context.MODE_PRIVATE);
		return settings.getString("idEquipo", "");
	}
	
	public void setIdEquipo(String idEquipo) {
		SharedPreferences settings = context.getSharedPreferences(NombrePref, Context.MODE_PRIVATE);
	    SharedPreferences.Editor editor = settings.edit();
	    editor.putString("idEquipo", idEquipo);
	    editor.commit();		
	}	


}
